<script>
'use strict';

const Service = require('egg').Service;

class <%=_.capitalize(name)%>Service extends Service {
  constructor(ctx) {
    super(ctx);
    this.name = '<%= name %>';
  }

  async create(params) {
    const { mysql } = this.app;
    try {
      const { <% attributes.forEach(item => { %> <%= item.name %>, <% }); %> } = params;
      const createTime = new Date();
      const result = await mysql.query("INSERT INTO <%= table || name %>(id, <% attributes.forEach(item => { %> <%= item.filed || item.name %>, <% }); %>created_time) VALUES (REPLACE(UUID(),'-',''), <% attributes.forEach(item => { %>?, <% }); %>?);", [ <% attributes.forEach(item => { %> <%= item.name %>, <% }); %>createTime ]);
      if (result.affectedRows === 1) {
        return result.insertId;
      }
    } catch (error) {
      this.ctx.throw(500, error.message);
    }
  }

  async find(params) {
    const { mysql } = this.app;
    const { helper: { pagination } } = this.ctx;
    try {
      const { <% attributes.forEach(item => { %> <%= item.name %>, <% }); %>page, size } = params;
      const pagger = pagination(page, size);
      const filters = {
        <% attributes.forEach(element => { %>
          <%= element.filed || element.name %>: {
            '=': <%= element.name %>
          },
        <% });%>
      };
      const segments = [];
      const values = [];
      Object.keys(filters).forEach(key => {
        const item = filters[key];
        const k = Object.keys(item)[0];
        if (item[k]) {
          segments.push(`(${key} ${k} ? )`);
          values.push(item[k]);
        }
      });

      const select = 'SELECT * FROM `<%= table || name %>`';
      const wheres = segments.length ? `WHERE ${segments.join(' OR ')}` : '';
      const order = 'ORDER BY `created_time` ASC';
      const limit = 'LIMIT ? OFFSET ?';

      const count = await mysql.query('SELECT COUNT(*) as total FROM `<%= table || name %>`' + wheres, [ ...values ]);
      const result = await mysql.query([ select, wheres, order, limit ].join(' '), [ ...values, pagger.limit, pagger.offset ]);

      return {
        pagination: {
          total: count[0].total,
          page: pagger.page,
          size: pagger.size,
        },
        list: result,
      };
    } catch (error) {
      this.ctx.throw(500);
    }
  }

  async findOne(params) {
    const { mysql } = this.app;
    try {
      const { id } = params;
      const result = await mysql.get('<%= table || name %>', { id });
      return result;
    } catch (error) {
      this.ctx.throw(500, error.message);
    }
  }

  async update(params) {
    const { mysql } = this.app;
    try {
      const { id, <% attributes.forEach(item => { %> <%= item.name %>, <% }); %> } = params;
      const updateTime = new Date();
      const result = await mysql.query('UPDATE `<%= table || name %>` SET <% attributes.forEach(item => { %>`<%= item.filed || item.name %>`=?, <% }); %>`updated_time`=? WHERE `id`=?', [ <% attributes.forEach(item => { %> <%= item.name %>, <% }); %>updateTime, id ]);
      if (result.affectedRows === 1) {
        return {};
      }
    } catch (error) {
      this.ctx.throw(500, error.message);
    }
  }

  async destroy(params) {
    const { mysql } = this.app;
    try {
      const { id } = params;
      const result = await mysql.delete('cms_category', { category_id: id });
      if (result.affectedRows === 1) {
        return {};
      }
    } catch (error) {
      this.ctx.throw(500, error.message);
    }
  }
}

module.exports = <%=_.capitalize(name)%>Service;
</script>
